/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OfficeOpenXMLInterop;

namespace Theme_Builder
{
    public partial class EditReflection : Form
    {
        public EditReflection()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets/Sets the CT_ReflectionEffect that this form is responsible for editing
        /// </summary>
        public OfficeOpenXMLInterop.CT_ReflectionEffect Reflection
        {
            get
            {
                CT_ReflectionEffect reflection = new CT_ReflectionEffect();

                reflection.algn = this.ConvertAlignment(this.comboBoxAlignment.SelectedItem.ToString());
                reflection.blurRad = (long)(float.Parse(this.textBoxBlurRad.Text) * 12700f);
                reflection.dir = (int)(float.Parse(this.textBoxOffsetDir.Text) * 60000f);
                reflection.dist = (long)(float.Parse(this.textBoxDist.Text) * 12700f);
                reflection.endA = (int)(float.Parse(this.textBoxAlphaEnd.Text) * 1000f);
                reflection.endPos = (int)(float.Parse(this.textBoxPosEnd.Text) * 1000f);
                reflection.fadeDir = (int)(float.Parse(this.textBoxFadeDir.Text) * 60000f);
                reflection.rotWithShape = this.checkBoxRotateWithShape.Checked;
                reflection.stA = (int)(float.Parse(this.textBoxAlphaStart.Text) * 1000f);
                reflection.stPos = (int)(float.Parse(this.textBoxPosStart.Text) * 1000f);

                int fooInt = (int)(float.Parse(this.textBoxSkewX.Text) * 60000f);
                if (fooInt != 0)
                {
                    reflection.kx = fooInt;
                }

                fooInt = (int)(float.Parse(this.textBoxSkewY.Text) * 60000f);
                if (fooInt != 0)
                {
                    reflection.ky = fooInt;
                }

                fooInt = (int)(float.Parse(this.textBoxScaleX.Text) * 1000f);
                if (fooInt != 100)
                {
                    reflection.sx = fooInt;
                }

                fooInt = (int)(float.Parse(this.textBoxScaleY.Text) * 1000f);
                if (fooInt != 100)
                {
                    reflection.sy = fooInt;
                }

                return reflection;
            }
            set
            {
                this.comboBoxAlignment.SelectedIndex = this.ConvertAlignmentToPosition(value.algn);

                this.checkBoxRotateWithShape.Checked = value.rotWithShape;

                this.textBoxBlurRad.Text = ((float)(value.blurRad) / 12700f).ToString();

                this.textBoxOffsetDir.Text = ((float)(value.dir) / 60000f).ToString();

                this.textBoxFadeDir.Text = ((float)(value.fadeDir) / 60000f).ToString();

                this.textBoxDist.Text = ((float)(value.dist) / 12700f).ToString();

                this.textBoxAlphaEnd.Text = ((float)(value.endA) / 1000f).ToString();
                this.textBoxAlphaStart.Text = ((float)(value.stA) / 1000f).ToString();

                this.textBoxPosEnd.Text = ((float)(value.endPos) / 1000f).ToString();
                this.textBoxPosStart.Text = ((float)(value.stPos) / 1000f).ToString();

                if (value.kx != 0)
                {
                    this.textBoxSkewX.Text = ((float)(value.kx) / 60000f).ToString();
                }

                if (value.ky != 0)
                {
                    this.textBoxSkewY.Text = ((float)(value.ky) / 60000f).ToString();
                }

                if (value.sx != 0 && value.sx != 100000)
                {
                    this.textBoxScaleX.Text = ((float)(value.sx) / 1000f).ToString();
                }

                if (value.sy != 0 && value.sy != 100000)
                {
                    this.textBoxScaleY.Text = ((float)(value.sy) / 1000f).ToString();
                }
            }
        }

        private OfficeOpenXMLInterop.ST_RectAlignment ConvertAlignment(string alignment)
        {
            switch (alignment)
            {
                case "Bottom":
                    return ST_RectAlignment.b;
                case "Bottom Left":
                    return ST_RectAlignment.bl;
                case "Bottom Right":
                    return ST_RectAlignment.br;
                case "Center":
                    return ST_RectAlignment.ctr;
                case "Left":
                    return ST_RectAlignment.l;
                case "Right":
                    return ST_RectAlignment.r;
                case "Top":
                    return ST_RectAlignment.t;
                case "Top Left":
                    return ST_RectAlignment.tl;
                case "Top Right":
                    return ST_RectAlignment.tr;
                default:
                    throw (new Exception("Unknown alignment - EditOuterShadow.ConvertAlignment: " + alignment));
            }
        }

        private int ConvertAlignmentToPosition(OfficeOpenXMLInterop.ST_RectAlignment alignment)
        {
            switch (alignment)
            {
                case ST_RectAlignment.b:
                    return 0;
                case ST_RectAlignment.bl:
                    return 1;
                case ST_RectAlignment.br:
                    return 2;
                case ST_RectAlignment.ctr:
                    return 3;
                case ST_RectAlignment.l:
                    return 4;
                case ST_RectAlignment.r:
                    return 5;
                case ST_RectAlignment.t:
                    return 6;
                case ST_RectAlignment.tl:
                    return 7;
                case ST_RectAlignment.tr:
                    return 8;
                default:
                    return -1;
            }
        }
    }
}